package com.tigercel.xiangchengyun.ui.activities

import android.os.Bundle
import android.provider.Settings
import android.support.v7.app.AppCompatActivity
import android.view.View
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener
import com.scwang.smartrefresh.layout.listener.OnRefreshListener
import com.tigercel.xiangchengyun.R
import com.tigercel.xiangchengyun.adapter.HotVideoAdapter
import com.tigercel.xiangchengyun.adapter.NewsBannerAdapter
import com.tigercel.xiangchengyun.entity.Banner
import com.tigercel.xiangchengyun.entity.HotVideoEntity
import com.tigercel.xiangchengyun.ui.custom.lbanners.LMBanners
import com.tigercel.xiangchengyun.ui.custom.lbanners.transformer.TransitionEffect
import com.tigercel.xiangchengyun.utils.Global
import com.tigercel.xiangchengyun.utils.JsonUtil
import com.tigercel.xiangchengyun.utils.PreferencesUtil
import com.tigercel.xiangchengyun.utils.TitleBuilder
import kotlinx.android.synthetic.main.activity_more.*
import kotlinx.android.synthetic.main.include_titlebar.*
import org.jetbrains.anko.toast

class MoreActivity : AppCompatActivity() {
    private var banner: LMBanners<Any>? = null
    private var headBanner: View? = null
    private lateinit var VideoList: MutableList<HotVideoEntity>
    private var VideoAdapter: HotVideoAdapter? = null
    private var title: String? = null
    private var localDats: PreferencesUtil?= null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_more)
        localDats = PreferencesUtil(this)
        title = intent.getStringExtra("title")
        when (title) {
            "火热快视频" -> getContentVideo(Global.baseUrl + Global.commonUrl)
            "电视栏目" -> getContentVideo(Global.baseUrl + Global.TvUrl)
            "百姓春晚" -> getContentVideo(Global.baseUrl + Global.festivalUrl)
        }
        headBanner = View.inflate(this, R.layout.banners, null)
        banner = headBanner?.findViewById(R.id.banners)
        banner!!.setIndicatorBottomPadding(8)
        banner!!.setHoriZontalTransitionEffect(TransitionEffect.Default)
        titlebar_tv.text = title
        titlebar_iv_left.visibility = View.VISIBLE
        TitleBuilder(this)
                .setLeftImage(R.drawable.btn_nav_back)
                .setLeftOnClickListener(View.OnClickListener {
                    this.finish()
                })
        refresh.setOnRefreshListener(OnRefreshListener {
            VideoList = ArrayList()
            when (title) {
                "火热快视频" -> getContentVideo(Global.baseUrl + Global.commonUrl)
                "电视栏目" -> getContentVideo(Global.baseUrl + Global.TvUrl)
                "百姓春晚" -> getContentVideo(Global.baseUrl + Global.festivalUrl)
            }
        })
        refresh.setOnLoadMoreListener(OnLoadMoreListener {
            when (title) {
                "火热快视频" -> getMoreVideo(Global.baseUrl + Global.commonUrlMore)
                "电视栏目" -> getMoreVideo(Global.baseUrl + Global.TvUrlMore)
                "百姓春晚" -> getMoreVideo(Global.baseUrl + Global.festivalUrlMore)
            }
        })
        refresh.autoRefresh()
    }

    private fun getContentVideo(url: String) {
        val deviceId = Settings.System.getString(this.contentResolver, Settings.System.ANDROID_ID)
        OkGo.get<String>(url)
                .tag(this)
                .params("channel_id", "12")
                .params("device_code", deviceId)
                .params("user_id", localDats!!.preferences["user_id"])
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>) {
                        val result = response!!.body()
                        val status = JsonUtil.get_key_string("status", result!!)
                        val msg = JsonUtil.get_key_string("msg", result!!)
                        val code = JsonUtil.get_key_string("code", result!!)

                        if (status == "0") {

//                            val model = JsonUtil.get_key_string("model", data)
//                            val show_type = JsonUtil.get_key_string("show_type", data)
                            if (title == "火热快视频") {
                                val data = JsonUtil.get_key_string("data", result!!)
                                val banners = JsonUtil.getList("banner", data)
                                val content = JsonUtil.getList("content", data)
                                if (banners.isNotEmpty()) {
                                    refresh.addView(headBanner)
                                    var banners1: MutableList<Banner>
                                    banners1 = ArrayList()
                                    banners1.clear()
                                    for (i in 0 until banners.size) {
                                        var img = JsonUtil.get_key_string("img", banners[i])
                                        var url = JsonUtil.get_key_string("url", banners[i])
                                        var name = JsonUtil.get_key_string("title", banners[i])
                                        var bannerBean = Banner(i, name, img, url)
                                        banners1.add(bannerBean)
                                    }
                                    banner!!.setAdapter(NewsBannerAdapter(applicationContext), banners1 as List<Any>?)
                                }
                                VideoList = ArrayList()
                                for (i in 0 until content.size) {
                                    var content_id = JsonUtil.get_key_string("content_id", content[i])
                                    var title = JsonUtil.get_key_string("title", content[i])
                                    var thumb_img = JsonUtil.get_key_string("thumb_img", content[i])
                                    var url = JsonUtil.get_key_string("url", content[i])
                                    var pub_time = JsonUtil.get_key_string("pub_time", content[i]).toLong()
                                    var hotVideoEntity = HotVideoEntity(content_id, title, thumb_img, url, pub_time)
                                    VideoList!!.add(hotVideoEntity)
                                }
                                VideoAdapter = HotVideoAdapter(this@MoreActivity, VideoList)
                                more_grid.adapter = VideoAdapter
                            } else if (title == "电视栏目") {
                                val data = JsonUtil.get_key_string("data", result!!)
                                val content = JsonUtil.getList("tv", data)
                                VideoList = ArrayList()
                                for (i in 0 until content.size) {
                                    var content_id = JsonUtil.get_key_string("content_id", content[i])
                                    var title = JsonUtil.get_key_string("title", content[i])
                                    var thumb_img = JsonUtil.get_key_string("thumb_img", content[i])
                                    var url = JsonUtil.get_key_string("url", content[i])
                                    var pub_time = JsonUtil.get_key_string("pub_time", content[i]).toLong()
                                    var hotVideoEntity = HotVideoEntity(content_id, title, thumb_img, url, pub_time)
                                    VideoList!!.add(hotVideoEntity)
                                }
                                VideoAdapter = HotVideoAdapter(this@MoreActivity, VideoList)
                                more_grid.adapter = VideoAdapter
                            } else {
                                val content = JsonUtil.getList("data", result!!)
                                VideoList = ArrayList()
                                for (i in 0 until content.size) {
                                    var content_id = JsonUtil.get_key_string("content_id", content[i])
                                    var title = JsonUtil.get_key_string("title", content[i])
                                    var thumb_img = JsonUtil.get_key_string("thumb_img", content[i])
                                    var url = JsonUtil.get_key_string("url", content[i])
                                    var pub_time = JsonUtil.get_key_string("pub_time", content[i]).toLong()
                                    var hotVideoEntity = HotVideoEntity(content_id, title, thumb_img, url, pub_time)
                                    VideoList!!.add(hotVideoEntity)
                                }
                                VideoAdapter = HotVideoAdapter(this@MoreActivity, VideoList)
                                more_grid.adapter = VideoAdapter
                            }
                            refresh.finishRefresh()
                        } else {
                            toast(getString(R.string.readFaile))
                        }
                    }
                })
    }

    private fun getMoreVideo(url: String) {
        val deviceId = Settings.System.getString(this.contentResolver, Settings.System.ANDROID_ID)
        OkGo.get<String>(url)
                .tag(this)
                .params("channel_id", "12")
                .params("device_code", deviceId)
                .params("page_size", "10")
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>) {
                        val result = response!!.body()
                        val status = JsonUtil.get_key_string("status", result!!)
                        val msg = JsonUtil.get_key_string("msg", result!!)
                        val code = JsonUtil.get_key_string("code", result!!)

                        if (title == "电视栏目") {
                            val data1 = JsonUtil.get_key_string("data", result!!)
                            if (status == "0") {
                                var VideoList1: MutableList<HotVideoEntity> = ArrayList()
                                val data = JsonUtil.getList("tv", data1)
                                if (data.isEmpty()) {
                                    refresh.finishLoadMoreWithNoMoreData()
                                } else {
                                    for (i in 0 until data.size) {
                                        var content_id = JsonUtil.get_key_string("content_id", data[i])
                                        var title = JsonUtil.get_key_string("title", data[i])
                                        var thumb_img = JsonUtil.get_key_string("thumb_img", data[i])
                                        var url = JsonUtil.get_key_string("url", data[i])
                                        var pub_time = JsonUtil.get_key_string("pub_time", data[i]).toLong()
                                        var hotVideoEntity = HotVideoEntity(content_id, title, thumb_img, url, pub_time)
                                        VideoList1!!.add(hotVideoEntity)
                                    }
                                    VideoAdapter!!.addAll(VideoList1)
                                    refresh.finishLoadMore()
                                }
                            } else {
                                toast(getString(R.string.readFaile) + code)
                            }
                        } else {
                            val data = JsonUtil.getList("data", result!!)
                            if (status == "0") {
                                var VideoList1: MutableList<HotVideoEntity> = ArrayList()
                                if (data.isEmpty()) {
                                    refresh.finishLoadMoreWithNoMoreData()
                                } else {
                                    for (i in 0 until data.size) {
                                        var content_id = JsonUtil.get_key_string("content_id", data[i])
                                        var title = JsonUtil.get_key_string("title", data[i])
                                        var thumb_img = JsonUtil.get_key_string("thumb_img", data[i])
                                        var url = JsonUtil.get_key_string("url", data[i])
                                        var pub_time = JsonUtil.get_key_string("pub_time", data[i]).toLong()
                                        var hotVideoEntity = HotVideoEntity(content_id, title, thumb_img, url, pub_time)
                                        VideoList1!!.add(hotVideoEntity)
                                    }
                                    VideoAdapter!!.addAll(VideoList1)
                                    refresh.finishLoadMore()
                                }

                            } else {
                                toast(getString(R.string.readFaile) )
                            }
                        }

                    }
                })
    }
}
